package action;

import Impl.TopicDaoImpl;
import entity.page.ListPage;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet(name = "ShowTopic",urlPatterns = "/show.jhtml")
public class ShowTopic extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("-------正在执行show----------");
        request.setCharacterEncoding("utf-8");
        String sname=request.getParameter("sname");
        request.setAttribute("sname",sname);
        int sid=Integer.parseInt(request.getParameter("sid"));
        TopicDaoImpl topicDao=new TopicDaoImpl();
        ArrayList<ListPage> topicInfos=topicDao.getTopicInfobyid(sid);
        ArrayList<ListPage> pages=new ArrayList<>();
        int pageNow = 0;
        if(request.getParameter("pageNow") == null) {
            pageNow = 1;
        }else {
            pageNow = Integer.parseInt(request.getParameter("pageNow"));
            System.out.println("当前："+pageNow);
        }
        int allCount = topicInfos.size();
        int allPage;
        if (allCount % 10 ==0) {
            allPage = allCount / 10;
        } else {
            allPage = allCount / 10 + 1;
        }
//        System.out.println("listpage"+topicInfos);
        for (int i = (pageNow - 1) * 10; i < pageNow * 10 && i < topicInfos.size(); i++) {
            pages.add(topicInfos.get(i));
//            System.out.println("ceshi"+topicInfos.get(i));
        }
//        System.out.println("hch"+pages);
        request.setAttribute("sid",sid);
        request.setAttribute("allpage", allPage);
        request.setAttribute("pageNow", pageNow);
        request.setAttribute("listPage", pages);
        request.getRequestDispatcher("/user/user_topic.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request,response);
    }
}
